|
1
|
|
|
import expect from 'expect'; |
|
2
|
|
|
import { |
|
3
|
|
|
getData, |
|
4
|
|
|
nameFromDataIndex, |
|
5
|
|
|
getValueFromDataIndexArr, |
|
6
|
|
|
setDataAtDataIndex, |
|
7
|
|
|
getRowKey |
|
8
|
|
|
} from './../../src/util/getData'; |
|
9
|
|
|
|
|
10
|
|
|
describe('The getRowKey utility function', () => { |
|
11
|
|
|
|
|
12
|
|
|
it('Should return an index when no unique prop is declared', () => { |
|
13
|
|
|
|
|
14
|
|
|
const columns = [ |
|
15
|
|
|
{ |
|
16
|
|
|
dataIndex: 'hat' |
|
17
|
|
|
}, |
|
18
|
|
|
{ |
|
19
|
|
|
dataIndex: 'phone' |
|
20
|
|
|
} |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
const rowValues = { |
|
24
|
|
|
hat: 'hattt', |
|
25
|
|
|
phone: '123' |
|
26
|
|
|
}; |
|
27
|
|
|
|
|
28
|
|
|
const index = 0; |
|
29
|
|
|
|
|
30
|
|
|
expect( |
|
31
|
|
|
getRowKey(columns, rowValues, index) |
|
32
|
|
|
).toEqual(0); |
|
33
|
|
|
|
|
34
|
|
|
}); |
|
35
|
|
|
|
|
36
|
|
|
it('Should return the value when unique prop is declared', () => { |
|
37
|
|
|
|
|
38
|
|
|
const columns = [ |
|
39
|
|
|
{ |
|
40
|
|
|
dataIndex: 'hat', |
|
41
|
|
|
createKeyFrom: true |
|
42
|
|
|
}, |
|
43
|
|
|
{ |
|
44
|
|
|
dataIndex: 'phone' |
|
45
|
|
|
} |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
const rowValues = { |
|
49
|
|
|
hat: 'hattt', |
|
50
|
|
|
phone: '123' |
|
51
|
|
|
}; |
|
52
|
|
|
|
|
53
|
|
|
const index = 0; |
|
54
|
|
|
|
|
55
|
|
|
expect( |
|
56
|
|
|
getRowKey(columns, rowValues, index) |
|
57
|
|
|
).toEqual('hattt'); |
|
58
|
|
|
|
|
59
|
|
|
}); |
|
60
|
|
|
|
|
61
|
|
|
it('Should work with a suffix', () => { |
|
62
|
|
|
|
|
63
|
|
|
const columns = [ |
|
64
|
|
|
{ |
|
65
|
|
|
dataIndex: 'hat', |
|
66
|
|
|
createKeyFrom: true |
|
67
|
|
|
}, |
|
68
|
|
|
{ |
|
69
|
|
|
dataIndex: 'phone' |
|
70
|
|
|
} |
|
71
|
|
|
]; |
|
72
|
|
|
|
|
73
|
|
|
const rowValues = { |
|
74
|
|
|
hat: 'hattt', |
|
75
|
|
|
phone: '123' |
|
76
|
|
|
}; |
|
77
|
|
|
|
|
78
|
|
|
const index = 0; |
|
79
|
|
|
|
|
80
|
|
|
expect( |
|
81
|
|
|
getRowKey(columns, rowValues, index, 'suffix') |
|
82
|
|
|
).toEqual('hattt-suffix'); |
|
83
|
|
|
|
|
84
|
|
|
}); |
|
85
|
|
|
|
|
86
|
|
|
it('Should throw an error if two cols declare createKeyFrom', () => { |
|
87
|
|
|
|
|
88
|
|
|
const columns = [ |
|
89
|
|
|
{ |
|
90
|
|
|
dataIndex: 'hat', |
|
91
|
|
|
createKeyFrom: true |
|
92
|
|
|
}, |
|
93
|
|
|
{ |
|
94
|
|
|
dataIndex: 'phone', |
|
95
|
|
|
createKeyFrom: true |
|
96
|
|
|
} |
|
97
|
|
|
]; |
|
98
|
|
|
|
|
99
|
|
|
const rowValues = { |
|
100
|
|
|
hat: 'hattt', |
|
101
|
|
|
phone: '123' |
|
102
|
|
|
}; |
|
103
|
|
|
|
|
104
|
|
|
const index = 0; |
|
105
|
|
|
|
|
106
|
|
|
expect(() => { |
|
107
|
|
|
getRowKey(columns, rowValues, index); |
|
108
|
|
|
}).toThrow('Only one column can declare createKeyFrom'); |
|
109
|
|
|
|
|
110
|
|
|
}); |
|
111
|
|
|
|
|
112
|
|
|
}); |
|
113
|
|
|
|
|
114
|
|
|
describe('nameFromDataIndex utility function', () => { |
|
115
|
|
|
|
|
116
|
|
|
it('Should work with a dataIndex', () => { |
|
117
|
|
|
|
|
118
|
|
|
const column = { |
|
119
|
|
|
dataIndex: 'someIndex', |
|
120
|
|
|
name: 'A thing' |
|
121
|
|
|
}; |
|
122
|
|
|
|
|
123
|
|
|
expect( |
|
124
|
|
|
nameFromDataIndex(column) |
|
125
|
|
|
).toEqual( |
|
126
|
|
|
'someIndex' |
|
127
|
|
|
); |
|
128
|
|
|
|
|
129
|
|
|
}); |
|
130
|
|
|
|
|
131
|
|
|
it('Should return empty string if no col is passed', () => { |
|
132
|
|
|
|
|
133
|
|
|
const column = null; |
|
134
|
|
|
|
|
135
|
|
|
expect( |
|
136
|
|
|
nameFromDataIndex(column) |
|
137
|
|
|
).toEqual( |
|
138
|
|
|
'' |
|
139
|
|
|
); |
|
140
|
|
|
|
|
141
|
|
|
}); |
|
142
|
|
|
|
|
143
|
|
|
it('Should work with a name', () => { |
|
144
|
|
|
|
|
145
|
|
|
const column = { |
|
146
|
|
|
name: 'A thing' |
|
147
|
|
|
}; |
|
148
|
|
|
|
|
149
|
|
|
expect( |
|
150
|
|
|
nameFromDataIndex(column) |
|
151
|
|
|
).toEqual( |
|
152
|
|
|
'aThing' |
|
153
|
|
|
); |
|
154
|
|
|
|
|
155
|
|
|
}); |
|
156
|
|
|
|
|
157
|
|
|
it('Should work with a string-array', () => { |
|
158
|
|
|
|
|
159
|
|
|
const column = { |
|
160
|
|
|
name: 'A thing', |
|
161
|
|
|
dataIndex: ['some', 'nested', 'value'] |
|
162
|
|
|
}; |
|
163
|
|
|
|
|
164
|
|
|
expect( |
|
165
|
|
|
nameFromDataIndex(column) |
|
166
|
|
|
).toEqual( |
|
167
|
|
|
'value' |
|
168
|
|
|
); |
|
169
|
|
|
|
|
170
|
|
|
}); |
|
171
|
|
|
|
|
172
|
|
|
}); |
|
173
|
|
|
|
|
174
|
|
|
describe('getData utility function', () => { |
|
175
|
|
|
|
|
176
|
|
|
it('Should return undefined if no column is present', () => { |
|
177
|
|
|
|
|
178
|
|
|
const rowData = { |
|
179
|
|
|
col1: 'banana', |
|
180
|
|
|
col2: 'orange', |
|
181
|
|
|
col3: 'apple' |
|
182
|
|
|
}; |
|
183
|
|
|
|
|
184
|
|
|
const columns = [ |
|
185
|
|
|
{ |
|
186
|
|
|
name: 'fruit', |
|
187
|
|
|
dataIndex: 'col1' |
|
188
|
|
|
}, |
|
189
|
|
|
{ |
|
190
|
|
|
name: 'another fruit', |
|
191
|
|
|
dataIndex: 'col2' |
|
192
|
|
|
}, |
|
193
|
|
|
{ |
|
194
|
|
|
name: 'yet another fruit', |
|
195
|
|
|
dataIndex: 'col3' |
|
196
|
|
|
} |
|
197
|
|
|
]; |
|
198
|
|
|
|
|
199
|
|
|
const colIndex = 3; |
|
200
|
|
|
|
|
201
|
|
|
expect( |
|
202
|
|
|
getData(rowData, columns, colIndex) |
|
203
|
|
|
).toEqual( |
|
204
|
|
|
undefined |
|
205
|
|
|
); |
|
206
|
|
|
|
|
207
|
|
|
}); |
|
208
|
|
|
|
|
209
|
|
|
it('Should throw an error if no dataIndex is defined', () => { |
|
210
|
|
|
|
|
211
|
|
|
const rowData = { |
|
212
|
|
|
col1: 'banana', |
|
213
|
|
|
col2: 'orange', |
|
214
|
|
|
col3: 'apple' |
|
215
|
|
|
}; |
|
216
|
|
|
|
|
217
|
|
|
const columns = [ |
|
218
|
|
|
{ |
|
219
|
|
|
name: 'fruit', |
|
220
|
|
|
dataIndex: 'col1' |
|
221
|
|
|
}, |
|
222
|
|
|
{ |
|
223
|
|
|
name: 'another fruit' |
|
224
|
|
|
}, |
|
225
|
|
|
{ |
|
226
|
|
|
name: 'yet another fruit', |
|
227
|
|
|
dataIndex: 'col3' |
|
228
|
|
|
} |
|
229
|
|
|
]; |
|
230
|
|
|
|
|
231
|
|
|
const colIndex = 1; |
|
232
|
|
|
|
|
233
|
|
|
expect(() => { |
|
234
|
|
|
getData(rowData, columns, colIndex); |
|
235
|
|
|
}).toThrow('No dataIndex found on column', columns[colIndex]); |
|
236
|
|
|
|
|
237
|
|
|
}); |
|
238
|
|
|
|
|
239
|
|
|
it('Should fetch data with a string key', () => { |
|
240
|
|
|
|
|
241
|
|
|
const rowData = { |
|
242
|
|
|
col1: 'banana', |
|
243
|
|
|
col2: 'orange', |
|
244
|
|
|
col3: 'apple' |
|
245
|
|
|
}; |
|
246
|
|
|
|
|
247
|
|
|
const columns = [ |
|
248
|
|
|
{ |
|
249
|
|
|
name: 'fruit', |
|
250
|
|
|
dataIndex: 'col1' |
|
251
|
|
|
}, |
|
252
|
|
|
{ |
|
253
|
|
|
name: 'another fruit', |
|
254
|
|
|
dataIndex: 'col2' |
|
255
|
|
|
}, |
|
256
|
|
|
{ |
|
257
|
|
|
name: 'yet another fruit', |
|
258
|
|
|
dataIndex: 'col3' |
|
259
|
|
|
} |
|
260
|
|
|
]; |
|
261
|
|
|
|
|
262
|
|
|
const colIndex = 1; |
|
263
|
|
|
|
|
264
|
|
|
expect(getData( |
|
265
|
|
|
rowData, |
|
266
|
|
|
columns, |
|
267
|
|
|
colIndex |
|
268
|
|
|
)).toEqual( |
|
269
|
|
|
'orange' |
|
270
|
|
|
); |
|
271
|
|
|
|
|
272
|
|
|
}); |
|
273
|
|
|
|
|
274
|
|
|
it('Should fetch data with a string-array key', () => { |
|
275
|
|
|
|
|
276
|
|
|
const rowData = { |
|
277
|
|
|
col1: { |
|
278
|
|
|
innerKey: 'inner orange' |
|
279
|
|
|
}, |
|
280
|
|
|
col2: 'orange', |
|
281
|
|
|
col3: 'apple' |
|
282
|
|
|
}; |
|
283
|
|
|
|
|
284
|
|
|
const columns = [ |
|
285
|
|
|
{ |
|
286
|
|
|
name: 'fruit', |
|
287
|
|
|
dataIndex: ['col1', 'innerKey'] |
|
288
|
|
|
}, |
|
289
|
|
|
{ |
|
290
|
|
|
name: 'another fruit', |
|
291
|
|
|
dataIndex: 'col2' |
|
292
|
|
|
}, |
|
293
|
|
|
{ |
|
294
|
|
|
name: 'yet another fruit', |
|
295
|
|
|
dataIndex: 'col3' |
|
296
|
|
|
} |
|
297
|
|
|
]; |
|
298
|
|
|
|
|
299
|
|
|
const colIndex = 0; |
|
300
|
|
|
|
|
301
|
|
|
expect(getData( |
|
302
|
|
|
rowData, |
|
303
|
|
|
columns, |
|
304
|
|
|
colIndex |
|
305
|
|
|
)).toEqual( |
|
306
|
|
|
'inner orange' |
|
307
|
|
|
); |
|
308
|
|
|
|
|
309
|
|
|
}); |
|
310
|
|
|
|
|
311
|
|
|
it('Should return empty string if string-array isnt valid', () => { |
|
312
|
|
|
|
|
313
|
|
|
const rowData = { |
|
314
|
|
|
col1: { |
|
315
|
|
|
innerKey: 'inner orange' |
|
316
|
|
|
}, |
|
317
|
|
|
col2: 'orange', |
|
318
|
|
|
col3: 'apple' |
|
319
|
|
|
}; |
|
320
|
|
|
|
|
321
|
|
|
const columns = [ |
|
322
|
|
|
{ |
|
323
|
|
|
name: 'fruit', |
|
324
|
|
|
dataIndex: ['col1', 'fakeKey'] |
|
325
|
|
|
}, |
|
326
|
|
|
{ |
|
327
|
|
|
name: 'another fruit', |
|
328
|
|
|
dataIndex: 'col2' |
|
329
|
|
|
}, |
|
330
|
|
|
{ |
|
331
|
|
|
name: 'yet another fruit', |
|
332
|
|
|
dataIndex: 'col3' |
|
333
|
|
|
} |
|
334
|
|
|
]; |
|
335
|
|
|
|
|
336
|
|
|
const colIndex = 0; |
|
337
|
|
|
|
|
338
|
|
|
expect(getData( |
|
339
|
|
|
rowData, |
|
340
|
|
|
columns, |
|
341
|
|
|
colIndex |
|
342
|
|
|
)).toEqual(''); |
|
343
|
|
|
|
|
344
|
|
|
}); |
|
345
|
|
|
}); |
|
346
|
|
|
|
|
347
|
|
|
describe('setDataAtDataIndex function', () => { |
|
348
|
|
|
|
|
349
|
|
|
it('Should work with nested object', () => { |
|
350
|
|
|
|
|
351
|
|
|
const rowValues = { |
|
352
|
|
|
outer: { |
|
353
|
|
|
inner: 'oldValue' |
|
354
|
|
|
} |
|
355
|
|
|
}; |
|
356
|
|
|
|
|
357
|
|
|
expect( |
|
358
|
|
|
setDataAtDataIndex( |
|
359
|
|
|
rowValues, |
|
360
|
|
|
['outer', 'inner'], |
|
361
|
|
|
'newValue' |
|
362
|
|
|
) |
|
363
|
|
|
).toEqual({ |
|
364
|
|
|
outer: { |
|
365
|
|
|
inner: 'newValue' |
|
366
|
|
|
} |
|
367
|
|
|
}); |
|
368
|
|
|
}); |
|
369
|
|
|
|
|
370
|
|
|
it('Should throw an error if an invalid key path is passed', () => { |
|
371
|
|
|
|
|
372
|
|
|
const rowValues = { |
|
373
|
|
|
'new': { |
|
374
|
|
|
thing: 'oldValue' |
|
375
|
|
|
} |
|
376
|
|
|
}; |
|
377
|
|
|
|
|
378
|
|
|
expect(() => { |
|
379
|
|
|
setDataAtDataIndex( |
|
380
|
|
|
rowValues, |
|
381
|
|
|
['new', 'invalidKey'], |
|
382
|
|
|
'newValue' |
|
383
|
|
|
); |
|
384
|
|
|
}).toThrow('Invalid key path'); |
|
385
|
|
|
}); |
|
386
|
|
|
|
|
387
|
|
|
it('Should work with a string', () => { |
|
388
|
|
|
|
|
389
|
|
|
const flatValues = { |
|
390
|
|
|
val: 1 |
|
391
|
|
|
}; |
|
392
|
|
|
|
|
393
|
|
|
expect( |
|
394
|
|
|
setDataAtDataIndex( |
|
395
|
|
|
flatValues, |
|
396
|
|
|
'val', |
|
397
|
|
|
'newValue' |
|
398
|
|
|
) |
|
399
|
|
|
).toEqual({ |
|
400
|
|
|
val: 'newValue' |
|
401
|
|
|
}); |
|
402
|
|
|
}); |
|
403
|
|
|
|
|
404
|
|
|
}); |
|
405
|
|
|
|
|
406
|
|
|
describe('getValueFromDataIndexArr function', () => { |
|
407
|
|
|
|
|
408
|
|
|
it('Should work with a nested object', () => { |
|
409
|
|
|
|
|
410
|
|
|
const rowData = { |
|
411
|
|
|
outer: { |
|
412
|
|
|
inner: { |
|
413
|
|
|
superInner: { |
|
414
|
|
|
value: 'x' |
|
415
|
|
|
} |
|
416
|
|
|
} |
|
417
|
|
|
} |
|
418
|
|
|
}; |
|
419
|
|
|
|
|
420
|
|
|
expect( |
|
421
|
|
|
getValueFromDataIndexArr( |
|
422
|
|
|
rowData, |
|
423
|
|
|
['outer', 'inner', 'superInner', 'value'] |
|
424
|
|
|
) |
|
425
|
|
|
).toEqual('x'); |
|
426
|
|
|
}); |
|
427
|
|
|
|
|
428
|
|
|
it('Should return empty string if the keys are invalid', () => { |
|
429
|
|
|
|
|
430
|
|
|
const rowData = { |
|
431
|
|
|
outer: { |
|
432
|
|
|
inner: { |
|
433
|
|
|
superInner: { |
|
434
|
|
|
value: 'x' |
|
435
|
|
|
} |
|
436
|
|
|
} |
|
437
|
|
|
} |
|
438
|
|
|
}; |
|
439
|
|
|
|
|
440
|
|
|
expect(getValueFromDataIndexArr( |
|
441
|
|
|
rowData, |
|
442
|
|
|
['outer', 'inner', 'fake', 'value'] |
|
443
|
|
|
)).toEqual(''); |
|
444
|
|
|
}); |
|
445
|
|
|
|
|
446
|
|
|
}); |
|
447
|
|
|
|